<template>
  <div class="container">
    <!-- 面包屑导航 -->
    <div class="breadcrumb">
      <el-breadcrumb separator="">
        <el-breadcrumb-item
          ><div class="div1">基本配置</div></el-breadcrumb-item
        >
        <el-breadcrumb-item
          ><div class="div2">菜单管理</div></el-breadcrumb-item
        >
      </el-breadcrumb>
    </div>
    <!-- 面包屑导航 teamManage -->
    <!-- 菜单栏 -->
    <div class="menuBtn">
      <el-tree
        class="el-menu-vertical-demo"
        :data="tableData"
        :props="defaultProps"
        :default-expand-all="true"
        :highlight-current="true"
        @node-click="handleNodeClick"
      ></el-tree>
    </div>
    <!-- 菜单栏 -->

    <!-- 管理团队 -->
    <div class="manage">
      <div class="btn">
        <el-button
          type="primary"
          size="mini"
          @click="oneLevelMenu"
          v-if="parArray.indexOf('addyimenu') != -1"
          >添加一级菜单</el-button
        >
        <!-- <el-button type='primary'>添加同级菜单</el-button> -->
        <el-button
          size="mini"
          type="primary"
          @click="lowerLevel"
          v-if="parArray.indexOf('addxia') != -1"
          >添加下级菜单</el-button
        >
        <el-button
          size="mini"
          type="danger"
          @click="delectData"
          v-if="parArray.indexOf('menudel') != -1"
          >删除菜单</el-button
        >
      </div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="elForm"
      >
        <el-form-item label="上级菜单" prop="father_name" v-if="if_father">
          <el-input v-model="ruleForm.father_name" disabled></el-input>
        </el-form-item>
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="菜单链接" prop="link">
          <el-input v-model="ruleForm.link"></el-input>
        </el-form-item>
        <el-form-item label="菜单编码" prop="code">
          <el-input v-model="ruleForm.code"></el-input>
        </el-form-item>
        <el-form-item label="菜单排序" prop="sortNo">
          <el-input
            type="number"
            style="width: 150px"
            v-model="ruleForm.sortNo"
          ></el-input>
        </el-form-item>
        <el-form-item label="菜单类型" prop="menuType">
          <el-select
            v-model="ruleForm.menuType"
            disabled
            placeholder="请选择菜单类型"
          >
            <el-option label="菜单" value="1"></el-option>
            <el-option label="按钮" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="系统类型" prop="systemType">
          <el-select v-model="ruleForm.systemType" placeholder="请选择系统类型">
            <el-option
              v-for="items in systemlist"
              :key="items.dictId"
              :label="items.dictName"
              :value="items.dictId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单图标" prop="icon">
          <el-input v-model="ruleForm.icon"></el-input>
        </el-form-item>
        <el-form-item label="权限标识" prop="identification">
          <el-input v-model="ruleForm.identification"></el-input>
        </el-form-item>
        <el-form-item label="菜单备注" prop="desc">
          <el-input type="textarea" v-model="ruleForm.desc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            size="mini"
            type="primary"
            @click="submitForm('ruleForm')"
            v-if="parArray.indexOf('ljcj') != -1"
            >立即创建</el-button
          >
          <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
        </el-form-item>
      </el-form>
    </div>
    <!-- 管理客户等级 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        link: "",
        sortNo: "",
        menuType: "1",
        desc: "",
        icon: "",
        code: "",
        father_name: "",
        identification: "",
        systemType: "1",
      },

      rules: {
        name: [{ required: true, message: "请输入菜单名称", trigger: "blur" }],
        link: [{ required: true, message: "请输入菜单链接", trigger: "blur" }],
        code: [{ required: true, message: "请输入菜单编码", trigger: "blur" }],
        sortNo: [
          { required: true, message: "请输入菜单排序", trigger: "blur" },
        ],
        icon: [{ required: true, message: "请输入菜单图标", trigger: "blur" }],
        desc: [{ required: true, message: "请填写菜单备注", trigger: "blur" }],
        // identification: [
        //   { required: true, message: '请填写菜单备注', trigger: 'blur' }
        // ]
      },
      currentPage1: 1,
      tableData: [],
      formpop: {
        list: [
          {
            name: "",
            code: "",
          },
        ],
        icon: "",
        link: "",
        sortNo: "",
        type: "",
      },
      formLabelWidth: "120px",
      if_delect: false,
      defaultProps: {
        children: "children",
        label: "name",
      },
      parentCode: "",
      if_father: false,
      menuId: "",
      limits: "",
      parArray: [],
      systemlist: [],
    };
  },
  created() {
    this.listAxios();
    this.systemAxios();
    // 权限设定判断 normsCode
    this.limits = JSON.parse(sessionStorage.getItem("limit"));
    const params = this.limits.buttonTreeDto.filter(
      (item) => item.menuCode === "menuCode"
    );
    params[0].buttonList.forEach((item) => {
      this.parArray.push(item.code);
    });
    // 权限设定判断 normsCode
  },
  activated() {
    //  this.listAxios();
  },
  methods: {
    // 系统下拉框字典组
    systemAxios() {
      this.axios
        .get("/sys/v1/common/win/type?groupId=win_global_type", {
          headers: {
            Authorization: "",
            isLoading: false,
          },
        })
        .then((res) => {
          // console.log(res);
          if (res.code == 0) {
            this.systemlist = res.data;
          } else {
            this.$message.error(res.message);
          }
        });
    },
    // 一级菜单
    oneLevelMenu() {
      this.parentCode = "";
      this.menuId = "";
      this.if_father = false;
      this.ruleForm = {
        name: "",
        link: "",
        sortNo: "",
        menuType: "1",
        desc: "",
        icon: "",
        code: "",
        identification: "",
        father_name: this.ruleForm.father_name,
        systemType: "1",
      };
    },
    // 添加下级菜单
    lowerLevel() {
      this.if_father = true;
      this.ruleForm = {
        name: "",
        link: "",
        sortNo: "",
        menuType: "1",
        desc: "",
        icon: "",
        code: "",
        identification: "",
        father_name: this.ruleForm.father_name,
        systemType: this.ruleForm.systemType,
      };
      this.menuId = "";
    },
    //** 查询菜单列表 **//
    listAxios() {
      this.axios
        .post("/sys/v1/menu/button/tree", {
          request: {
            name: "",
          },
          version: "",
        })
        .then((res) => {
          // console.log("查询菜单列表");
          // console.log(res);
          if (res.code == 0) {
            this.tableData = res.data;
          } else {
            this.$message.error(res.message);
          }
        });
    },
    submitForm: _.debounce(function (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.menuId == "") {
            this.axios
              .post("/sys/v1/menu/button/add", {
                request: {
                  userId: sessionStorage.getItem("user_id"),
                  name: this.ruleForm.name,
                  code: this.ruleForm.code,
                  parentCode: this.parentCode,
                  type: this.ruleForm.menuType,
                  iconUrl: this.ruleForm.icon,
                  jumpUrl: this.ruleForm.link,
                  sort: this.ruleForm.sortNo,
                  remark: this.ruleForm.desc,
                  perms: this.ruleForm.identification,
                  winType: this.ruleForm.systemType,
                },
              })
              .then((res) => {
                console.log(res);
                if (res.code == 0) {
                  this.listAxios();
                  this.$message.success("添加成功");
                  this.if_father = false;
                  this.menuId == "";
                  this.ruleForm = {
                    name: "",
                    link: "",
                    sortNo: "",
                    menuType: "1",
                    desc: "",
                    icon: "",
                    code: "",
                    father_name: "",
                    identification: "",
                    systemType: "1",
                  };
                } else {
                  this.$message.error(res.message);
                }
              });
          } else {
            // 修改按钮
            this.axios
              .post("/sys/v1/menu/button/update", {
                request: {
                  id: this.menuId,
                  userId: sessionStorage.getItem("user_id"),
                  name: this.ruleForm.name,
                  code: this.ruleForm.code,
                  parentCode: this.parentCode,
                  type: this.ruleForm.menuType,
                  iconUrl: this.ruleForm.icon,
                  jumpUrl: this.ruleForm.link,
                  sort: this.ruleForm.sortNo,
                  remark: this.ruleForm.desc,
                  perms: this.ruleForm.identification,
                  winType: this.ruleForm.systemType,
                },
              })
              .then((res) => {
                // console.log(res);
                if (res.code == 0) {
                  this.listAxios();
                  this.$message.success("修改成功");
                  this.if_father = false;
                  this.menuId == "";
                  this.ruleForm = {
                    name: "",
                    link: "",
                    sortNo: "",
                    menuType: "1",
                    desc: "",
                    icon: "",
                    code: "",
                    father_name: "",
                    identification: "",
                    systemType: "1",
                  };
                } else {
                  this.$message.error(res.message);
                }
              });
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    }, 300),
    //** 查询菜单列表 **//
    handleNodeClick(data) {
      // console.log(data);
      this.parentCode = data.code;
      this.menuId = data.id;
      this.ruleForm = {
        father_name: data.name,
        name: data.name,
        link: data.jumpUrl,
        sortNo: data.sort,
        menuType: "1",
        desc: data.remark,
        icon: data.iconUrl,
        code: data.code,
        identification: data.perms,
        systemType: data.winType,
      };
    },
    is_null(content) {
      if (content) {
        return false;
      } else {
        return true;
      }
    },
    delectData() {
      this.$confirm("确定要删除该权限吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (this.menuId == "" || this.menuId == undefined) {
            this.$message.warning("删除失败");
          } else {
            this.axios
              .post(
                "/sys/v1/menu/button/delete",
                {
                  request: {
                    userId: sessionStorage.getItem("user_id"),
                    id: this.menuId,
                  },
                },
                {
                  headers: {
                    isloading: false,
                  },
                }
              )
              .then((res) => {
                console.log(res);
                if (res.code == 0) {
                  this.$message.success("删除成功");
                  // this.tableData.list.splice(index, 1);
                  this.listAxios();
                  this.parentCode = "";
                  this.if_father = false;
                  this.menuId = "";
                  this.ruleForm = {
                    name: "",
                    link: "",
                    sortNo: "",
                    menuType: "1",
                    desc: "",
                    icon: "",
                    code: "",
                    father_name: "",
                    identification: "",
                    systemType: "1",
                  };
                } else {
                  this.$message.error(res.message);
                }
              });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // handleCurrentChange(val) {
    // 		// console.log(`当前页: ${val}`);
    // 		this.staiffList(`${val}`);
    // },
    //删除当前行数据
    deleteRow(index, rows) {
      console.log(index);
      rows.splice(index, 1);
    },
    tomenuEditing(row) {
      this.$router.push({ name: "menuEditing", query: { id: row.id } });
    },
    handleEdit(index, row) {
      console.log(index, row);
      //row是该行tableData对应的一行
      this.form = row;
    },
    /*** 增加按钮/链接 ***/
    addContent() {
      let content = {
        name: "",
        icon: "",
      };
      this.formpop.list.push(content);
      if (this.formpop.list.length >= 2) {
        this.if_delect = true;
      } else {
        this.if_delect = false;
      }
    },
    /*** 增加按钮/链接 ***/

    /*** 删掉按钮/链接 ***/
    delect(index) {
      this.formpop.list.splice(index, 1);
      if (this.formpop.list.length >= 2) {
        this.if_delect = true;
      } else {
        this.if_delect = false;
      }
    },
    /*** 删掉按钮/链接 ***/
  },
};
</script>

<style scoped lang="scss">
.div1 {
  margin-left: -16px;
  background: url(../../../assets/breadcrumb2.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #2d7edf;
}
.div2 {
  margin-left: -16px;
  background: url(../../../assets/breadcrumb1.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #fff;
}
.div3 {
  margin-left: -8px;
  background: url(../../../assets/breadcrumb3.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #2d7edf;
}

.el-menu-vertical-demo {
  width: 200px;
  position: absolute;
  left: 0;
  height: 95%;
  overflow-y: scroll;
}
.elForm {
  padding: 20px 10px;
}
.formBox {
  z-index: 10000;
  position: relative;
}
.header {
  font-size: 16px;
  color: #2d7edf;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}
.demo-form-inline {
  margin-top: 10px;
}
/deep/.el-input__inner {
  height: 30px;
  line-height: 30px;
}
.manage {
  margin-top: 20px;
  padding: 10px;
  background: #fff;
  position: absolute;
  top: 24px;
  left: 230px;
}
/deep/.has-gutter .cell {
  text-align: center;
  color: #2d7ddf;
}
/deep/.cell {
  text-align: center;
}
.name {
  font-size: 16px;
  color: #2d7edf;
  margin: 10px 0;
}
.optain {
  width: 100%;
  text-align: center;
  p {
    display: inline-block;
    vertical-align: middle;
    margin-left: 20px;
    img {
      width: 18px;
      height: 20px;
      margin-top: 8px;
    }
  }
}
.item {
  display: inline-block;
  margin-left: 10px;
  position: relative;
  img {
    width: 22px;
    height: 22px;
  }
}
.pover {
  display: inline-block;
  /deep/.el-button {
    padding: 8px;
  }
}
/deep/.el-form-item {
  // margin-bottom:0px;
}
/deep/.el-popover__title {
  font-size: 15px;
  color: #666;
}
.float {
  padding: 8px 10px !important;
}
.page {
  margin: 20px 0;
  text-align: right;
  width: 100%;
  span {
    font-size: 15px;
    color: #888;
    display: inline-block;
    margin-left: 10px;
    margin-top: -2px;
    vertical-align: middle;
  }
  .el-pagination {
    display: inline-block;
    vertical-align: middle;
  }
}
/deep/.el-table__row:hover {
  color: rgb(45, 125, 223);
}
/deep/.el-dialog {
  width: 55%;
  .el-input__inner {
    width: 50%;
  }
}
.add_btn {
  margin-bottom: 20px;
  /deep/.el-input__inner {
    width: 200px;
  }
  .el-form-item,
  .box {
    display: inline-block;
    vertical-align: middle;
    // width:300px;
  }
  img {
    display: inline-block;
    vertical-align: middle;
    margin-left: 20px;
    width: 22px;
    height: 22px;
  }
  p {
    display: inline-block;
    padding-top: 5px;
  }
  .delect {
    display: inline-block;
    // padding-top:10px;
    img {
      width: 23px;
      height: 23px;
    }
  }
}
.btn {
  width: 550px;
}
</style>
